package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFavorMapper dishFavorMapper;

    @Autowired
    private SetDishMapper setDishMapper;
    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void SaveWithFavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();//获取口味列表
        if(flavors != null && flavors.size() > 0){
            flavors.stream().forEach(f -> f.setDishId(dish.getId()));
            //批量传入口味数据
            dishFavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除菜品
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletebyId(List<Long> ids) {
        //正在售卖的不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getByid(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //已经关联套餐的菜品不能删除
        List<Long> setidbyDish = setDishMapper.getSetidbyDish(ids);
        if(setidbyDish != null && setidbyDish.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除 首先批量删除菜品 接着批量删除菜品的口味
        dishMapper.deleteBatchByid(ids);
        dishFavorMapper.deleteBatchByid(ids);
    }

    /**
     * 根据id回显菜品信息 包括口味
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public DishVO getDishWithFavor(Long id) {
        DishVO dishVO = new DishVO();
        //要查询到菜品信息
        Dish dish = dishMapper.getByid(id);

        //要查询到菜品口味信息
        List<DishFlavor> flavors = dishFavorMapper.getByDishId(id);

        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO ;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        //修改菜品
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //先删除口味信息
        List<Long> favorIds = new ArrayList<>();
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            favorIds.add(flavor.getDishId());
        }
        dishFavorMapper.deleteBatchByid(favorIds);

        //再进行口味信息的添加
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.stream().forEach(f -> f.setDishId(dish.getId()));
            dishFavorMapper.insertBatch(flavors);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    /**
     * 设置菜品的状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
        if(status == StatusConstant.DISABLE){//如果是停售的话 要把套餐也进行停售
            List<Long> setidbyDish = setDishMapper.getSetidbyDish(Arrays.asList(id));
            if(setidbyDish != null && setidbyDish.size() > 0){
                //当前菜品关联的套餐
                for (Long l : setidbyDish) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(l);
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setDishMapper.updateByid(setmeal);
                }
            }

        }
    }

    @Override
    public List<Dish> list(DishDTO dishDTO) {
        dishDTO.setStatus(StatusConstant.ENABLE);
        return dishMapper.list(dishDTO);
    }

    /**
     * 获取起售中的菜品列表带口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listByDish(Dish dish) {
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        List<DishVO> dishVOList = new ArrayList<>();
        List<Dish> dishList = dishMapper.list(dishDTO); //获取起售中的菜品列表
        for (Dish dish1 : dishList) {
            DishVO dishVO = new DishVO();
            List<DishFlavor> dishFlavor = dishFavorMapper.getByDishId(dish1.getId());
            BeanUtils.copyProperties(dish1,dishVO);
            dishVO.setFlavors(dishFlavor);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

}
